package com.suntimes.guangjiegou.fragment;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.suntimes.cl.util.CLLog;
import com.suntimes.guangjiegou.R;
import com.suntimes.guangjiegou.component.TabBar;
import com.suntimes.guangjiegou.component.TabBar.OnClickTabItemListener;
import com.suntimes.guangjiegou.component.TabBarItem;
import com.suntimes.guangjiegou.fragment.collection.CollectionFragment;
import com.suntimes.guangjiegou.fragment.collocation.CollocationFragment;
import com.suntimes.guangjiegou.fragment.hot.HotFragment;
import com.suntimes.guangjiegou.fragment.settings.SettingsFragment;
import com.suntimes.guangjiegou.fragment.shopping.ShoppingFragment;

public class TabFragment extends NavigationSubFragment implements OnClickTabItemListener {
	
	public static final String TAG = "TabFragment";
	public static final int TAB_INDEX_HOT = 0;
	public static final int TAB_INDEX_COLLOCATION = 1;
	public static final int TAB_INDEX_SHOPPING = 2;
	public static final int TAB_INDEX_COLLECTION = 3;
	public static final int TAB_INDEX_SETTINGS = 4;
	
	protected TabBar mTabBar;
	private int mCurrentTabIndex;
//	private ViewGroup mContainerView;
	
	private List<BaseFragment> mTabFragmentList;
	private int[] mFragmentTitleArray;
	private BaseFragment mCurrentFragment;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = super.onCreateView(inflater, container, savedInstanceState);
		setup();
		return view;
	}

	@Override
	protected View loadView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.page_tab, null, false);
		
		mTabBar = (TabBar)view.findViewById(R.id.page_tab_tabBar);
		mTabBar.setOnClickTabItemListener(this);
		return view;
	}
	
	private void setup() {
		mTabFragmentList = new ArrayList<BaseFragment>();
		
		HotFragment hotFragment = new HotFragment();
		hotFragment.setNavigationFragment(getNavigationFragment());
		CollocationFragment collocationFragment = new CollocationFragment();
		collocationFragment.setNavigationFragment(getNavigationFragment());
		ShoppingFragment shoppingFragment = new ShoppingFragment();
		shoppingFragment.setNavigationFragment(getNavigationFragment());
		CollectionFragment collectionFragment = new CollectionFragment();
		collectionFragment.setNavigationFragment(getNavigationFragment());
		SettingsFragment settingsFragment = new SettingsFragment();
		settingsFragment.setNavigationFragment(getNavigationFragment());
		
		mTabFragmentList.add(hotFragment);
		mTabFragmentList.add(collocationFragment);
		mTabFragmentList.add(shoppingFragment);
		mTabFragmentList.add(collectionFragment);
		mTabFragmentList.add(settingsFragment);
		
		mFragmentTitleArray = new int[5];
		mFragmentTitleArray[0] = R.string.tab_renmen;
		mFragmentTitleArray[1] = R.string.tab_tapei;
		mFragmentTitleArray[2] = R.string.tab_guangjie;
		mFragmentTitleArray[3] = R.string.tab_shoucang;
		mFragmentTitleArray[4] = R.string.tab_shezhi;
		
		
		selecteTab(TAB_INDEX_SHOPPING);
		mTabBar.selectTab(TAB_INDEX_SHOPPING);
	}
	
	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	
	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	
	@Override
	public void onDestroy() {
		mTabBar = null;
		super.onDestroy();
	}

	@Override
	public void onClickTabItem(TabBarItem tabItem) {
		selecteTab(tabItem.getTabIndex());
	}
	
	public void selecteTab(int tabIndex) {
		if(mCurrentTabIndex != tabIndex) {
			mCurrentTabIndex = tabIndex;
			CLLog.i(TAG, "selecteTab >> " + tabIndex);
			
			FragmentManager fragmentManager = this.getChildFragmentManager();
			FragmentTransaction transaction = fragmentManager.beginTransaction();
			
			if(mCurrentFragment != null) {
				transaction.hide(mCurrentFragment);
				mCurrentFragment.onPause();
			}
			
			mCurrentFragment = mTabFragmentList.get(tabIndex);
			String fragmentTag = getFragmentKey(mCurrentFragment);
			BaseFragment selectedFragment = (BaseFragment)fragmentManager.findFragmentByTag(fragmentTag);
			if(selectedFragment == null) {
				transaction.add(R.id.page_tab_containerView, mCurrentFragment, fragmentTag);
			} else {
				transaction.show(mCurrentFragment);
				mCurrentFragment.onResume();
			}
			transaction.commit();
			this.setTitle(mFragmentTitleArray[tabIndex]);
		}
	}
	
	private String getFragmentKey(BaseFragment fragment){
		return fragment.getClass().getName();
	}

}
